package com.lzt.admin.common.security.handler;

import com.lzt.admin.api.security.SecurityAPI;
import com.lzt.admin.common.redis.config.RedisCore;
import com.lzt.admin.common.utils.PrintResponseUtil;
import com.lzt.admin.common.utils.SecurityVerifyUtil;
import com.lzt.admin.vo.UserDetailsVO;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RequiredArgsConstructor
public class LztAuthenticationSuccessHandler implements AuthenticationSuccessHandler, SecurityAPI {
    private final PrintResponseUtil printResponseUtil;
    private final SecurityVerifyUtil securityVerifyUtil;
    private final RedisCore redisCache;
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        UserDetailsVO user = (UserDetailsVO) authentication.getPrincipal();
        String retryKey = securityVerifyUtil.getRetryKey(request);
        securityVerifyUtil.clearLoginRecordCache(retryKey);
        String uuid = request.getParameter("uuid");
        user.getUserDTO().setUuid(uuid);
        String uuidKey = MENU_CODE_KEY + uuid;
//        redisCache.setCacheObject(uuidKey,user.getMenuListAll(),6, TimeUnit.HOURS);
        printResponseUtil.printSuccess(request,response,user);
    }
}
